package com.wang.chatbot.api.domain.zsxq.service.Imp;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.wang.chatbot.api.domain.zsxq.model.aggregates.UnAnsweredQuestionsAggregates;
import com.wang.chatbot.api.domain.zsxq.model.req.AnswerReq;
import com.wang.chatbot.api.domain.zsxq.model.req.ReqData;
import com.wang.chatbot.api.domain.zsxq.model.res.AnswerRes;
import com.wang.chatbot.api.domain.zsxq.service.IZsxqApi;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @author Your Name
 * @date 2025/11/8 12:29
 * <Description of the class>
 */
@Service
public class ZsxqApi implements IZsxqApi {
    Logger logger = LoggerFactory.getLogger(ZsxqApi.class);
    @Override
    public UnAnsweredQuestionsAggregates queryUnAnsweredQuestionsTopicId(String cookie) throws Exception {
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpGet get=new HttpGet("https://api.zsxq.com/v2/topics/55188248144254424/comments?sort=asc&count=30&with_sticky=true");
        get.addHeader("cookie", cookie);
        get.addHeader("Content-Type", "application/json;charset=UTF-8\n");
        CloseableHttpResponse response = client.execute(get);
        if (response.getStatusLine().getStatusCode()== 200){
            logger.info("拉取问题");
            String jsonStr= EntityUtils.toString(response.getEntity(), "UTF-8");
            //把json转为实体类
            return JSON.parseObject(jsonStr, UnAnsweredQuestionsAggregates.class);
        }else {
            throw  new RuntimeException("queryUnAnseredQuestionTopicId err code is "+response.getStatusLine().getStatusCode());
        }
    }

    @Override
    public boolean answer(String cookie, String text) throws IOException {
        CloseableHttpClient client = HttpClientBuilder.create().build();
        HttpPost post = new HttpPost("https://api.zsxq.com/v2/topics/55188248144254424/comments");
        post.addHeader("cookie", cookie);
        post.addHeader("user-agent","Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/142.0.0.0 Safari/537.36 Edg/142.0.0.0");
        post.addHeader("Content-Type", "application/json;charset=UTF-8\n");
        //构造请求体
        AnswerReq answerReq = new AnswerReq(new ReqData(text, null, null));
        String paramJson = JSON.toJSONString(answerReq);
        //实体转为转为json字符串
        StringEntity param=new StringEntity(paramJson, ContentType.create("application/json", "UTF-8"));
        post.setEntity(param);
        CloseableHttpResponse response = client.execute(post);
        if (response.getStatusLine().getStatusCode()== 200){
            AnswerRes answerRes = JSON.parseObject(EntityUtils.toString(response.getEntity(), "UTF-8"), AnswerRes.class);
            logger.info("回答星球问题");
            return answerRes.isSucceeded();
        }else {
            throw  new RuntimeException("queryUnAnseredQuestionTopicId err code is "+response.getStatusLine().getStatusCode());
        }
    }
}
